//
//  Chapter.m
//  ebook
//
//  Created by 吴超飞 on 16/2/25.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "Chapter.h"

@implementation Chapter

-(id)initWithDictionary:(NSDictionary *)aDict{
    
    self = [super init];
    if (self) {
        self.chapterid = [NSString stringWithFormat:@"%@", aDict[@"id"]] ;
        self.chaptername = aDict[@"title"];
        self.chapterorder = aDict[@"order"];
        self.sourceurl = aDict[@"source"];
        self.chaptertype = aDict[@"type"];
        self.updatetime = aDict[@"updateTime"];
        if ([self.sourceurl isKindOfClass:[NSNull class]]) {
            self.sourceurl = @"";
        }
        while ([self.sourceurl rangeOfString:@"%"].location != NSNotFound)//url可能是url编码的，需要解码
            self.sourceurl = [self.sourceurl stringByRemovingPercentEncoding];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    if (self = [super init]) {
        self.chapterid = [aDecoder decodeObjectForKey:@"chapterid"];
        self.chaptername = [aDecoder decodeObjectForKey:@"chaptername"];
        self.chapterorder = [aDecoder decodeObjectForKey:@"chapterorder"];
        self.sourceurl = [aDecoder decodeObjectForKey:@"sourceurl"];
        self.chaptertype = [aDecoder decodeObjectForKey:@"chaptertype"];
        self.updatetime = [aDecoder decodeObjectForKey:@"updatetime"];
    }
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder{
    
    [aCoder encodeObject:self.chapterid forKey:@"chapterid"];
    [aCoder encodeObject:self.chaptername forKey:@"chaptername"];
    [aCoder encodeObject:self.chapterorder forKey:@"chapterorder"];
    [aCoder encodeObject:self.sourceurl forKey:@"sourceurl"];
    [aCoder encodeObject:self.chaptertype forKey:@"chaptertype"];
    [aCoder encodeObject:self.updatetime forKey:@"updatetime"];
}

@end
